﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XapSpyAnalysis;

namespace XapSpyAnalysisWPF
{
	class GraphicStatistic
	{
		public Statistic Statistic { get; set; }
		public DateTime[] Dates { get; set; }
		public int[] MethodNumbers { get; set; }
		public Dictionary<int, string> MethodHashDictionary { get; set; }

		/// <summary>
		/// Empty constructor.
		/// </summary>
		public GraphicStatistic()
		{

		}

		/// <summary>
		/// Constructor with direct initilization.
		/// </summary>
		/// <param name="Statistic"></param>
		public GraphicStatistic(Statistic statistic)
		{
			Statistic = statistic;
			// create arrays for Dates and MethodNumbers
			Dates = new DateTime[statistic.DataList.Count];
			MethodNumbers = new int[statistic.DataList.Count];
			MethodHashDictionary = new Dictionary<int, string>();
			buildArrays();
		}
		
		/// <summary>
		/// Builds the necessary Arrays for the plotted graph.
		/// </summary>
		private void buildArrays()
		{
			MethodHashDictionary = new Dictionary<int,string>();
			// initialize counter and methodNumber
			int count = 0;
			int methodNumber = 0;

            // go through the complete DataList and over all entries
			for (int i = 0; i < Statistic.DataList.Count; ++i)
			{
				Dates[i] = Statistic.DataList[i].Time;
				string methodName = Statistic.DataList[i].MethodName;
				// compare Statistic.ListOfUniqueMethodStrings with appearence in methodName
				if (Statistic.ListOfUniqueMethodStrings.Contains(methodName))
				{
					// if methodName exists in ListOfUniqueMethodStrings && not exist in Dictionary,
                    // assign a new int number 
					// and save in Dictionary<int, string> as key with value MethodName
                    if (!MethodHashDictionary.ContainsValue(methodName))
                    {
					    MethodHashDictionary.Add(count, methodName);
					    methodNumber = count;
					    // store count in methodNumber so that it can be added to the array
					    // count one up for the next Dictionary entry
					    count++;
                    }
                    else
                    {
                        // the methodNumber exists already and need to be reassigned
                        // we need to find the correct methodNumber for the given methodName
                        foreach (KeyValuePair<int, string> item in MethodHashDictionary)
                        {
                            if (item.Value == methodName)
                            {
                                // methodNumber found
                                methodNumber = item.Key;
                            }
                        }
                    }
				}
				MethodNumbers[i] = methodNumber;
			}
		}
	}
}
